﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using zdzh.zdmgr.BLL;
using zdzh.zdmgr.COL;
using zdzh.zdmgr.SFL;

namespace zdzh.zdmgr.UIL.wuyefeip
{
    /// <summary>
    /// 对象名称：“回迁户模块”添加信息页面（用户界面层）
    /// 对象说明：该页面用于向数据库中添加“回迁户”对象，操作完成后返回列表页面。
    /// 作者姓名：飞翔熊
    /// 编写日期：2012/12/3 13:23:16
    /// </summary>
    public partial class Create : System.Web.UI.Page
    {
        protected Wuyefei wuyefei=new Wuyefei();
        protected List<Huiqianhu> huiqianhu;
        protected Fangyuan fangyuan;
        protected List<Wuyefei> wuyefeiList;

        /// <summary>
        /// “回迁户模块”添加信息页面载入时的事件处理方法。
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            LblDate.Text = DateTime.Now.ToString("yyyy年MM月dd日");
            string index = Request.QueryString["Index"];
            fangyuan = FangyuanBLL.GetDataByIndex(index);
            if (fangyuan.XieyiIndex.HasValue)
                huiqianhu = HuiqianhuBLL.GetDataByXieyihao(fangyuan.XieyiIndex.Value);
            wuyefeiList = WuyefeiBLL.GetDataByFangyuanIndex(fangyuan.Index);
            if (wuyefeiList == null )
            {

            }
            if (!Page.IsPostBack)
            {
                BindObjectToForm();  // 将关联对象默认值，绑定至界面控件进行显示。
            }
        }

        /// <summary>
        /// 用户单击“保存”按钮时的事件处理方法。
        /// </summary>
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            BindFormlToObject();
            WuyefeiBLL.Insert(wuyefei);
            Main.SysMessage.ShowSuccessMsg("“物业费”添加成功，单击“确定”返回信息列表。",
                "../fangyuanp/Detail.aspx?Index=" + fangyuan.Index); // 添加完成后，返回列表中的最后一页。
        }

        #region 界面控件与关联对象之间的绑定方法

        /// <summary>
        /// 将界面控件中的值，绑定给关联对象。
        /// </summary>
        private void BindFormlToObject()
        {
            //if (!DataValid.IsNullOrInt(TxtXieyihao.Text))
            //    throw new CustomException("“协议号”不是一个有效的整数，请您重新输入。");

            wuyefei.fangyuanindex = fangyuan.Index;
            wuyefei.date = DateTime.Now;
            wuyefei.xingming = LblOwner.Text;
            wuyefei.wuyefei = DataValid.GetNullOrDecimal(TxtWuyefei.Text);
            wuyefei.qunuanfei = DataValid.GetNullOrDecimal(TxtQunuanfei.Text);
            wuyefei.gongshuifei = DataValid.GetNullOrDecimal(TxtGongshuifei.Text);
            wuyefei.diantifei = DataValid.GetNullOrDecimal(TxtDiantifei.Text);
            wuyefei.zongji = Convert.ToDecimal(TxtZongji.Text);
            wuyefei.startdate = DataValid.GetNullOrDateTime(TxtStartDate.Text);
            wuyefei.enddate = DataValid.GetNullOrDateTime(TxtUntilDate.Text);
        }

        /// <summary>
        /// 将关联对象中的值，绑定至界面控件进行显示。
        /// </summary>
        private void BindObjectToForm()
        {
            LblBuild.Text = fangyuan.getBuild() + " " + fangyuan.getType();
            LblArea.Text = fangyuan.Area.ToString() + "㎡";
            LblOwner.Text = "";
            if (huiqianhu!=null)
            {
                foreach (Huiqianhu model in huiqianhu)
                {
                    if (LblOwner.Text != "")
                        LblOwner.Text += " ";
                    LblOwner.Text += model.Xingming;
                }
            }

            decimal wuyefeihouse = 3.6M;
            decimal wuyefeishangfu = 3.6M;
            decimal qunuanfeihouse = 28;
            decimal qunuanfeishangfu = 36;
            decimal gongshuifeihouse = 80;
            decimal gongshuifeicheku = 40;
            decimal diantifei;
            switch (fangyuan.Type)
            {
                case 1: //多层
                    LblWuyeJia.Text = wuyefeihouse.ToString() + "／㎡·年";
                    TxtWuyefei.Text = (Math.Round(fangyuan.Area.Value * wuyefeihouse, 0, MidpointRounding.AwayFromZero)).ToString("N0");
                    LblQunuanJia.Text = qunuanfeihouse.ToString() + "／㎡·６个月";
                    TxtQunuanfei.Text = (Math.Round(fangyuan.Area.Value * qunuanfeihouse, 0, MidpointRounding.AwayFromZero)).ToString("N0");
                    LblGongshuiJia.Text = gongshuifeihouse.ToString() + "／户·年";
                    TxtGongshuifei.Text = gongshuifeihouse.ToString("N0");
                    LblDiantiJia.Text = "";
                    TxtDiantifei.Text = null;
                    break;
                case 2: // 车库
                    LblWuyeJia.Text = wuyefeihouse.ToString() + "／㎡·年";
                    TxtWuyefei.Text = (Math.Round(fangyuan.Area.Value * wuyefeihouse, 0, MidpointRounding.AwayFromZero)).ToString("N0");
                    LblQunuanJia.Text = qunuanfeihouse.ToString() + "／㎡·６个月";
                    TxtQunuanfei.Text = (Math.Round(fangyuan.Area.Value * qunuanfeihouse, 0, MidpointRounding.AwayFromZero)).ToString("N0");
                    LblGongshuiJia.Text = gongshuifeicheku.ToString() + "／户·年";
                    TxtGongshuifei.Text = gongshuifeicheku.ToString("N0");
                    LblDiantiJia.Text = "";
                    TxtDiantifei.Text = null;
                    break;
                case 3: // 商服
                    LblWuyeJia.Text = wuyefeishangfu.ToString() + "／㎡·年";
                    TxtWuyefei.Text = (Math.Round(fangyuan.Area.Value * wuyefeishangfu, 0, MidpointRounding.AwayFromZero)).ToString("N0");
                    LblQunuanJia.Text = qunuanfeishangfu.ToString() + "／㎡·６个月";
                    TxtQunuanfei.Text = (Math.Round(fangyuan.Area.Value * qunuanfeishangfu, 0, MidpointRounding.AwayFromZero)).ToString("N0");
                    LblGongshuiJia.Text = gongshuifeihouse.ToString() + "／户·年";
                    TxtGongshuifei.Text = gongshuifeihouse.ToString("N0");
                    LblDiantiJia.Text = "";
                    TxtDiantifei.Text = null;
                    break;
                case 4: // 阁楼
                    LblWuyeJia.Text = (wuyefeihouse * 0.8M).ToString() + "／㎡·年";
                    TxtWuyefei.Text = (Math.Round(fangyuan.Area.Value*0.8M * wuyefeihouse, 0, MidpointRounding.AwayFromZero)).ToString("N0");
                    LblQunuanJia.Text = (qunuanfeihouse * 0.8M).ToString() + "／㎡·６个月";
                    TxtQunuanfei.Text = (Math.Round(fangyuan.Area.Value*0.8M * qunuanfeihouse, 0, MidpointRounding.AwayFromZero)).ToString("N0");
                    LblGongshuiJia.Text = gongshuifeihouse.ToString() + "／户·年";
                    TxtGongshuifei.Text = gongshuifeihouse.ToString("N0");
                    LblDiantiJia.Text = "";
                    TxtDiantifei.Text = null;
                    break;
                case 5: // 高层
                    LblWuyeJia.Text = wuyefeihouse.ToString() + "／㎡·年";
                    TxtWuyefei.Text = (Math.Round(fangyuan.Area.Value * wuyefeihouse, 0, MidpointRounding.AwayFromZero)).ToString("N0");
                    LblQunuanJia.Text = qunuanfeihouse.ToString() + "／㎡·６个月";
                    TxtQunuanfei.Text = (Math.Round(fangyuan.Area.Value * qunuanfeihouse, 0, MidpointRounding.AwayFromZero)).ToString("N0");
                    LblGongshuiJia.Text = gongshuifeihouse.ToString() + "／户·年";
                    TxtGongshuifei.Text = gongshuifeihouse.ToString("N0");

                    diantifei=0.4M;
                    if(fangyuan.Build=="G13")
                    {
                        diantifei += ((int)(Convert.ToInt32(fangyuan.Floor) - 3) / 2) * 0.05M;
                    }
                    else
                    {
                        diantifei += ((int)(Convert.ToInt32(fangyuan.Floor) - 2) / 2) * 0.05M;
                    }
                    diantifei *= 12M;
                    LblDiantiJia.Text = diantifei.ToString() + "／㎡·年";
                    TxtDiantifei.Text = (Math.Round(fangyuan.Area.Value * diantifei, 0, MidpointRounding.AwayFromZero)).ToString("N0");
                    break;
                default:
                    break;
            }

            SetZongjia();
        }

        #endregion 界面控件与关联对象之间的绑定方法

        private void SetZongjia()
        {
            decimal wuyefei = Convert.ToDecimal(TxtWuyefei.Text);
            decimal qunuanfei = Convert.ToDecimal(TxtQunuanfei.Text);
            decimal gongshuifei = Convert.ToDecimal(TxtGongshuifei.Text);
            decimal diantifei;

            decimal zongjia = wuyefei + qunuanfei + gongshuifei;
            if (fangyuan.Type==5)
            {
                diantifei = Convert.ToDecimal(TxtDiantifei.Text);
                zongjia += diantifei;
            }

            TxtZongji.Text = zongjia.ToString("N0");

            LblZongji.Text = DataValid.ConvertToChineseMoney(Convert.ToDouble(zongjia));
        }

        protected void TxtWuyefei_TextChanged(object sender, EventArgs e)
        {
            SetZongjia();
        }

        protected void TxtQunuanfei_TextChanged(object sender, EventArgs e)
        {
            SetZongjia();
        }

        protected void TxtGongshuifei_TextChanged(object sender, EventArgs e)
        {
            SetZongjia();
        }

        protected void TxtDiantifei_TextChanged(object sender, EventArgs e)
        {
            SetZongjia();
        }
        
    }
}
